/*==============================================================================
STATA DO-FILE: MASTER REPLICATION SCRIPT
PROJECT:       Pandemic Digital Media and Adolescents' Parenthood Desires
PURPOSE:       This file preprocesses the data and replicates all tables, 
               figures, and supplementary analyses for the manuscript submission 
               to Demographic Research.
AUTHOR:        Seunghwan (Shawn) Yoo
DATE:          September 11, 2025
==============================================================================*/

clear all

// --- SECTION 0: LOAD DATA ---
// NOTE: Please replace the placeholder path with the actual file path on your machine.
use "[FILE PATH]/Social_Media_Fertility_Weight_2018-2023.dta", clear


/*==============================================================================
SECTION 1: DATA PREPROCESSING AND VARIABLE CONSTRUCTION
- This section creates all variables used in the final analysis.
==============================================================================*/

// 1.1. Dependent Variable: Parenthood Desire (3 categories)
gen Desired_Children_Recode_new = .
replace Desired_Children_Recode_new = 1 if inlist(Desired_Children, 1) // 0 Children
replace Desired_Children_Recode_new = 2 if inlist(Desired_Children, 2, 3, 4, 5, 6, 7) // 1+ Children
replace Desired_Children_Recode_new = 3 if Desired_Children == 8 // Don't know
label define Desired_Children_lbl_new 1 "0 Child" 2 "1+ Child" 3 "Don't know"
label values Desired_Children_Recode_new Desired_Children_lbl_new

// 1.2. Key Independent Variable: Pandemic Period
gen Covid_Period = .
replace Covid_Period = 1 if Survey_Year <= 2020 // Pre-Pandemic (2018-2020)
replace Covid_Period = 2 if Survey_Year > 2020  // Post-Pandemic (2021-2023)
label define Covid_Period_lbl 1 "Pre-Pandemic" 2 "Post-Pandemic"
label values Covid_Period Covid_Period_lbl

// 1.3. Sample Selection & Control Variables
gen Gender_Recode = .
replace Gender_Recode = 1 if Gender == 1 // Male
replace Gender_Recode = 2 if Gender == 2 // Female

gen Race_Recode = .
replace Race_Recode = 1 if Race == 2 // White (ref)
replace Race_Recode = 2 if Race == 1 // Black
replace Race_Recode = 3 if Race == 3 // Hispanic
replace Race_Recode = 4 if Race > 3 | Race == . // Other/NA
label define Race_lbl 1 "White" 2 "Black" 3 "Hispanic" 4 "Other/NA"
label values Race_Recode Race_lbl

gen City_Size_Recode = .
replace City_Size_Recode = 1 if inlist(City_Size, 4, 5, 6) // Small/Med City (ref)
replace City_Size_Recode = 2 if inlist(City_Size, 2, 3) // Rural
replace City_Size_Recode = 3 if inlist(City_Size, 7, 8, 9, 10) // Large City
replace City_Size_Recode = 4 if City_Size == 1 | City_Size == . // Mixed/Unknown
label define City_Size_lbl 1 "Small/Med City" 2 "Rural" 3 "Large City" 4 "Mixed/Unknown"
label values City_Size_Recode City_Size_lbl

gen Combined_Living_Arrangement = .
replace Combined_Living_Arrangement = 1 if Lives_with_Mother == 2 & Lives_with_Father == 2 // Both Parents (ref)
replace Combined_Living_Arrangement = 2 if Lives_with_Mother != 2 & Lives_with_Father == 2 // Father Only
replace Combined_Living_Arrangement = 3 if Lives_with_Mother == 2 & Lives_with_Father != 2 // Mother Only
replace Combined_Living_Arrangement = 4 if (Lives_with_Mother != 2 & Lives_with_Father != 2) | (Lives_with_Mother == . | Lives_with_Father == .) // Neither/NA
label define Living_Arrange_lbl 1 "Both Parents" 2 "Father Only" 3 "Mother Only" 4 "Neither/NA"
label values Combined_Living_Arrangement Living_Arrange_lbl

gen Poli_Beli_grp = .
replace Poli_Beli_grp = 1 if inlist(Poli_Beli, 1, 2) // Conservative
replace Poli_Beli_grp = 2 if Poli_Beli == 3 // Moderate (ref)
replace Poli_Beli_grp = 3 if inlist(Poli_Beli, 4, 5, 6) // Liberal
replace Poli_Beli_grp = 4 if Poli_Beli == 7 // None/Don't know
label define PoliBeliGrp 1 "Conservative" 2 "Moderate" 3 "Liberal" 4 "None/Don't know"
label values Poli_Beli_grp Poli_Beli_grp

// 1.4. Media Use Variables
// Social Media is treated as categorical based on the final decision.
gen Social_Media_Hours_Cat = .
replace Social_Media_Hours_Cat = 1 if Social_Media_Hours == 1 // None (ref)
replace Social_Media_Hours_Cat = 2 if Social_Media_Hours == 2 // < 1 hour
replace Social_Media_Hours_Cat = 3 if inlist(Social_Media_Hours, 3, 4) // 1-4 hours
replace Social_Media_Hours_Cat = 4 if inlist(Social_Media_Hours, 5, 6, 7) // 5+ hours
label define SM_lbl 1 "None" 2 "< 1 hour" 3 "1-4 hours" 4 "5+ hours"
label values Social_Media_Hours_Cat SM_lbl
// NOTE: Weekday_Video_Hours, Weekend_Video_Hours, Gaming_Hours are used as 1-7 scale continuous variables.

// 1.5. Finalize Analytical Sample
keep if Gender_Recode == 1 | Gender_Recode == 2
svyset [pweight=ARCHIVE_WT]

// 1.6. Define Global Macros for Cleaner Code
global controls "Religion_Importance Siblings Happiness_Level Date_Wk ib(1).City_Size_Recode ib(1).Race_Recode ib(1).Combined_Living_Arrangement ib(2).Mother_Education ib(2).Father_Education ib(2).Poli_Beli_grp"
global media_vars "i.Social_Media_Hours_Cat c.Weekday_Video_Hours c.Weekend_Video_Hours c.Gaming_Hours"


/*==============================================================================
SECTION 2: MAIN ANALYSIS - REPLICATION OF MANUSCRIPT TABLES
- Replicates Table 1, 2, and 3 by running separate interaction models.
- Provides values to construct the final tables in the manuscript.
==============================================================================*/

di " "
di "*** SECTION 2: REPLICATING MAIN TABLES ***"

// --- 2.1. Pooled Sample Models (for Table 1) ---
di "--- TABLE 1: POOLED SAMPLE ---"
// Model for Weekday Screen
mlogit Desired_Children_Recode_new $media_vars ib(1).Covid_Period $controls c.Weekday_Video_Hours#i.Covid_Period [pweight=ARCHIVE_WT], baseoutcome(1)
margins, dydx(Weekday_Video_Hours) at(Covid_Period=(1 2))
margins r.Covid_Period, dydx(Weekday_Video_Hours)

// Model for Weekend Screen
mlogit Desired_Children_Recode_new $media_vars ib(1).Covid_Period $controls c.Weekend_Video_Hours#i.Covid_Period [pweight=ARCHIVE_WT], baseoutcome(1)
margins, dydx(Weekend_Video_Hours) at(Covid_Period=(1 2))
margins r.Covid_Period, dydx(Weekend_Video_Hours)

// Model for Gaming
mlogit Desired_Children_Recode_new $media_vars ib(1).Covid_Period $controls c.Gaming_Hours#i.Covid_Period [pweight=ARCHIVE_WT], baseoutcome(1)
margins, dydx(Gaming_Hours) at(Covid_Period=(1 2))
margins r.Covid_Period, dydx(Gaming_Hours)


// --- 2.2. Men Sample Models (for Table 2) ---
di "--- TABLE 2: MEN SAMPLE ---"
// Run all three models for men to generate all values for the table
mlogit Desired_Children_Recode_new $media_vars ib(1).Covid_Period $controls c.Weekday_Video_Hours#i.Covid_Period [pweight=ARCHIVE_WT] if Gender_Recode==1, baseoutcome(1)
margins, dydx(Weekday_Video_Hours) at(Covid_Period=(1 2))
margins r.Covid_Period, dydx(Weekday_Video_Hours)

mlogit Desired_Children_Recode_new $media_vars ib(1).Covid_Period $controls c.Weekend_Video_Hours#i.Covid_Period [pweight=ARCHIVE_WT] if Gender_Recode==1, baseoutcome(1)
margins, dydx(Weekend_Video_Hours) at(Covid_Period=(1 2))
margins r.Covid_Period, dydx(Weekend_Video_Hours)

mlogit Desired_Children_Recode_new $media_vars ib(1).Covid_Period $controls c.Gaming_Hours#i.Covid_Period [pweight=ARCHIVE_WT] if Gender_Recode==1, baseoutcome(1)
margins, dydx(Gaming_Hours) at(Covid_Period=(1 2))
margins r.Covid_Period, dydx(Gaming_Hours)


// --- 2.3. Women Sample Models (for Table 3) ---
di "--- TABLE 3: WOMEN SAMPLE ---"
// Run all three models for women to generate all values for the table
mlogit Desired_Children_Recode_new $media_vars ib(1).Covid_Period $controls c.Weekday_Video_Hours#i.Covid_Period [pweight=ARCHIVE_WT] if Gender_Recode==2, baseoutcome(1)
margins, dydx(Weekday_Video_Hours) at(Covid_Period=(1 2))
margins r.Covid_Period, dydx(Weekday_Video_Hours)

mlogit Desired_Children_Recode_new $media_vars ib(1).Covid_Period $controls c.Weekend_Video_Hours#i.Covid_Period [pweight=ARCHIVE_WT] if Gender_Recode==2, baseoutcome(1)
margins, dydx(Weekend_Video_Hours) at(Covid_Period=(1 2))
margins r.Covid_Period, dydx(Weekend_Video_Hours)

mlogit Desired_Children_Recode_new $media_vars ib(1).Covid_Period $controls c.Gaming_Hours#i.Covid_Period [pweight=ARCHIVE_WT] if Gender_Recode==2, baseoutcome(1)
margins, dydx(Gaming_Hours) at(Covid_Period=(1 2))
margins r.Covid_Period, dydx(Gaming_Hours)


/*==============================================================================
SECTION 3: APPENDIX - FULL REGRESSION MODELS
- Replicates Appendix Tables A2, A3, A4 (Full Model Outputs).
==============================================================================*/

di " "
di "*** SECTION 3: REPLICATING APPENDIX FULL MODELS ***"

// --- 3.1. Appendix Table A2: Pooled Sample, Weekend Screen Model ---
di "--- APPENDIX TABLE A2: POOLED SAMPLE, WEEKEND SCREEN ---"
mlogit Desired_Children_Recode_new $media_vars ib(1).Covid_Period $controls c.Weekend_Video_Hours#i.Covid_Period [pweight=ARCHIVE_WT], baseoutcome(1)

// --- 3.2. Appendix Table A3: Men Sample, Weekend Screen Model ---
di "--- APPENDIX TABLE A3: MEN SAMPLE, WEEKEND SCREEN ---"
mlogit Desired_Children_Recode_new $media_vars ib(1).Covid_Period $controls c.Weekend_Video_Hours#i.Covid_Period [pweight=ARCHIVE_WT] if Gender_Recode==1, baseoutcome(1)

// --- 3.3. Appendix Table A4: Women Sample, Gaming Model ---
di "--- APPENDIX TABLE A4: WOMEN SAMPLE, GAMING ---"
mlogit Desired_Children_Recode_new $media_vars ib(1).Covid_Period $controls c.Gaming_Hours#i.Covid_Period [pweight=ARCHIVE_WT] if Gender_Recode==2, baseoutcome(1)


/*==============================================================================
SECTION 4: APPENDIX - SENSITIVITY ANALYSIS
- Replicates Supplementary Figures S1 and S2 (Year-by-Year Effects).
==============================================================================*/

di " "
di "*** SECTION 4: REPLICATING APPENDIX SENSITIVITY ANALYSIS ***"

// --- 4.1. Figure S1: Men, Weekend Screen Time by Year ---
di "--- FIGURE S1: MEN, WEEKEND SCREEN ---"
mlogit Desired_Children_Recode_new $media_vars $controls c.Weekend_Video_Hours#i.Survey_Year [pweight=ARCHIVE_WT] if Gender_Recode==1, baseoutcome(1)
margins, dydx(Weekend_Video_Hours) at(Survey_Year=(2018 2019 2020 2021 2022 2023)) predict(outcome(2)) post

// --- 4.2. Figure S2: Women, Gaming by Year ---
di "--- FIGURE S2: WOMEN, GAMING ---"
mlogit Desired_Children_Recode_new $media_vars $controls c.Gaming_Hours#i.Survey_Year [pweight=ARCHIVE_WT] if Gender_Recode==2, baseoutcome(1)
margins, dydx(Gaming_Hours) at(Survey_Year=(2018 2019 2020 2021 2022 2023)) predict(outcome(2)) post

/*==============================================================================
SECTION 5: APPENDIX - DESCRIPTIVE STATISTICS
- Replicates Appendix Table A1 (Descriptive Statistics).
==============================================================================*/

di " "
di "*** SECTION 5: REPLICATING APPENDIX DESCRIPTIVE STATISTICS ***"

// --- 5.1. Total Sample ---
di "--- DESCRIPTIVES: TOTAL SAMPLE ---"
svy: mean Weekday_Video_Hours Weekend_Video_Hours Gaming_Hours Religion_Importance Siblings Happiness_Level Date_Wk
svy: tab Desired_Children_Recode_new Social_Media_Hours_Cat Race_Recode City_Size_Recode, percent

// --- 5.2. By Pandemic Period ---
di "--- DESCRIPTIVES: PRE-PANDEMIC (2018-2020) ---"
svy, subpop(if Covid_Period == 1): mean Weekday_Video_Hours Weekend_Video_Hours Gaming_Hours
svy, subpop(if Covid_Period == 1): tab Desired_Children_Recode_new Social_Media_Hours_Cat Race_Recode, percent

di "--- DESCRIPTIVES: POST-PANDEMIC (2021-2023) ---"
svy, subpop(if Covid_Period == 2): mean Weekday_Video_Hours Weekend_Video_Hours Gaming_Hours
svy, subpop(if Covid_Period == 2): tab Desired_Children_Recode_new Social_Media_Hours_Cat Race_Recode, percent

// --- 5.3. By Gender ---
di "--- DESCRIPTIVES: MEN ---"
svy, subpop(if Gender_Recode == 1): mean Weekday_Video_Hours Weekend_Video_Hours Gaming_Hours
svy, subpop(if Gender_Recode == 1): tab Desired_Children_Recode_new Social_Media_Hours_Cat Race_Recode, percent

di "--- DESCRIPTIVES: WOMEN ---"
svy, subpop(if Gender_Recode == 2): mean Weekday_Video_Hours Weekend_Video_Hours Gaming_Hours
svy, subpop(if Gender_Recode == 2): tab Desired_Children_Recode_new Social_Media_Hours_Cat Race_Recode, percent

// --- END OF MASTER REPLICATION FILE ---